package com.bq.gametrade.base;

import android.Manifest;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.bq.gametrade.GameTradeApplication;
import com.bq.gametrade.R;
import com.bq.gametrade.ui.dialog.CustomerServiceDialog;
import com.bq.gametrade.ui.dialog.LoadingProgressDialog;

import java.lang.ref.WeakReference;

/**
 * @author Feel on 2016/11/24 14:49
 */

public  class BaseActivity extends FragmentActivity {

    protected GameTradeApplication application;

    private Toast toast;

    protected Handler mHandler;
    protected boolean isDestroy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.title_bg));

        }
        init();
        application = GameTradeApplication.getInstance();

    }

    private void init(){
        mHandler = new MyHandler(this);

    }


    protected LoadingProgressDialog dialog ;

    public void showSpotsDialog() {
        if (dialog == null) {
            dialog = new LoadingProgressDialog(this);
        }
        dialog.show();
    }

    public void showSpotsDialog(String title) {
        if (dialog == null) {
            dialog = new LoadingProgressDialog(this,title);
        }
        dialog.show();
    }

    public void closeSpotsDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog.cancel();
        }
    }

    public void showCustomerService(int id){
        ImageView imageView = (ImageView)findViewById(id);
        imageView.setVisibility(View.VISIBLE);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomerServiceDialog dialog = new CustomerServiceDialog();
                dialog.show(getSupportFragmentManager(),"dialog");
            }
        });

    }

    protected <T extends View> T serchViewById(int id){
        return (T)findViewById(id);
    }

    /**
     * Toast
     * @param text
     */
    public void toastShow( String text) {
        if(toast == null)
        {
            toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        }
        else {
            toast.setText(text);
        }
        toast.show();
    }


    /**
     * 接收处理mHandler的消息
     */
    protected void performHandleMessage(Message msg) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy = true;
        mHandler.removeCallbacksAndMessages(null);
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private static class MyHandler extends Handler {
        private WeakReference<Context> reference;

        public MyHandler(Context context) {
            reference = new WeakReference<>(context);
        }
        @Override
        public void handleMessage(Message msg) {
            BaseActivity baseActivity = (BaseActivity) reference.get();
            if (!baseActivity.isDestroy) {
                baseActivity.performHandleMessage(msg);
            }

        }
    }



    private static final int REQUEST_PERM = 151;

    /***
     * 动态授权
     */
    public void checkPermission(){

        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_SMS,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,


            };
            ActivityCompat.requestPermissions(this,mPermissionList,REQUEST_PERM);
        }

    }



}
